<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 顶部说明 -->
				<view class="instruction">
					<text class="instruction-title">时间起卦</text>
					<text class="instruction-text">基于您选择的时间点来生成卦象，时间蕴含着天地运行的奥秘</text>
				</view>
				
				<!-- 问题输入 -->
				<app-card>
					<view class="question-section">
						<text class="section-title">您的问题</text>
						<textarea 
							v-model="question" 
							placeholder="请输入您想要占卜的问题..."
							class="question-input"
							maxlength="200"
						></textarea>
					</view>
				</app-card>
				
				<!-- 时间选择 -->
				<app-card>
					<view class="time-section">
						<text class="section-title">当前时间</text>
						<view class="current-time-display">
							<text class="time-display">{{ currentTimeStr }}</text>
							<text class="time-desc">基于当前时间生成卦象</text>
						</view>
					</view>
				</app-card>
				
				<!-- 起卦按钮 -->
				<view class="action-section">
					<app-button 
						:text="'开始起卦'" 
						type="primary"
						@tap="generateHexagram"
						:disabled="!question"
					></app-button>
				</view>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import apiService from '@/utils/api-service.js'

export default {
	name: 'TimeDivinationPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			question: '',
			currentTime: new Date()
		}
	},
	computed: {
		currentTimeStr() {
			const date = this.currentTime;
			return this.formatDateTime(date);
		}
	},
	onLoad() {
		this.updateCurrentTime();
	},
	onUnload() {
		if (this.timeInterval) {
			clearInterval(this.timeInterval);
		}
	},
	methods: {
		updateCurrentTime() {
			this.timeInterval = setInterval(() => {
				this.currentTime = new Date();
			}, 1000);
		},
		
		formatDateTime(date) {
			const year = date.getFullYear();
			const month = String(date.getMonth() + 1).padStart(2, '0');
			const day = String(date.getDate()).padStart(2, '0');
			const hour = String(date.getHours()).padStart(2, '0');
			const minute = String(date.getMinutes()).padStart(2, '0');
			const second = String(date.getSeconds()).padStart(2, '0');
			
			return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
		},
		
		async generateHexagram() {
			if (!this.question) {
				uni.showToast({
					title: '请输入问题',
					icon: 'none'
				});
				return;
			}
			
			// 检查网络状态
			const isNetworkAvailable = await apiService.checkNetworkStatus();
			if (!isNetworkAvailable) {
				uni.showToast({
					title: '网络连接不可用',
					icon: 'none'
				});
				return;
			}
			
			uni.showLoading({
				title: '正在起卦...'
			});
			
			try {
				const response = await apiService.timeDivination({
					question: this.question,
					timestamp: this.currentTime.getTime(),
					selectedTime: this.formatDateTime(this.currentTime)
				});
				
				uni.hideLoading();
				
				if (response.success) {
					// 跳转到结果页面
					uni.navigateTo({
						url: `/pages/divination-result/divination-result?type=time&question=${encodeURIComponent(this.question)}&result=${encodeURIComponent(JSON.stringify(response.data))}&selectedTime=${encodeURIComponent(this.formatDateTime(this.currentTime))}`
					});
				} else {
					uni.showToast({
						title: response.message || '起卦失败',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('起卦失败:', error);
				uni.hideLoading();
				apiService.handleError(error);
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.instruction {
	text-align: center;
	margin-bottom: 48rpx;
	
	.instruction-title {
		display: block;
		font-size: 48rpx;
		font-weight: bold;
		color: #000000;
		margin-bottom: 16rpx;
	}
	
	.instruction-text {
		display: block;
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.7);
		line-height: 1.5;
	}
}

.question-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 24rpx;
	}
	
	.question-input {
		width: 100%;
		min-height: 120rpx;
		padding: 24rpx;
		border: 2rpx solid #f0f0f0;
		border-radius: 16rpx;
		font-size: 28rpx;
		line-height: 1.5;
		background-color: #fafafa;
	}
}

.time-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 24rpx;
	}
	
	.current-time-display {
		text-align: center;
		padding: 40rpx 0;
		
		.time-display {
			display: block;
			font-size: 36rpx;
			font-weight: bold;
			color: #6A82FB;
			margin-bottom: 16rpx;
		}
		
		.time-desc {
			display: block;
			font-size: 28rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.action-section {
	margin-top: 48rpx;
	text-align: center;
}
</style>